<?php
namespace app\api\controller;

use app\common\controller\Super;

class Base extends Super
{
    protected function getuser($only_user_id=true){
        $token=input("token");
        $user_id= cache("user_id_$token");
        if(empty($user_id)){
            e("登录已失效",300);
        }

        cache("user_id_bytoken_{$user_id}",$token,86400*7);
        cache("user_id_$token",$user_id,86400*7);

        if($only_user_id){
            return $user_id;
        }
        $user=model("user")->where("id",$user_id)->find()->toArray();
        if(empty($user)){
            e("登录已失效",300);
        }


        return $user;
    }
    protected function bindgroup($user_id,$group_code){

        if(empty($group_code)){
            return false;
        }

        $group=model("group")->where("code",$group_code)->find();

        if(empty($group)){
            return false;
        }
        $group_code=intval($group_code);
        if($group_code==0){
            return false;
        }
        $user_group=model("group")->where("user_id",$user_id)->find();
        if($group_code==$user_group['code']){
            return false;
        }
        $user=model("user")->where("id",$user_id)->find();
        $up_code=null;
        $time=time();
        if($user['group_code']!=null &&$user['bind_timestamp']>($time-1296000)){
           return false;
        }

        if($user['group_code']!=null){
            $up_code=$user['group_code'];
        }

        model("user")->where("id",$user['id'])->update(['group_code'=>$group_code]);
        $this->setgroup_user_number($up_code);
        $this->setgroup_user_number($group_code);

        return $group;

    }


}
